﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.Meterial
{
    public partial class SupplementaryMaterial : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                DropDownList_CenterId.Items.Clear();
                var myquery = from a in db.TABC_JS_CostCenter select a;
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID), myquery);

                Panel_MainQuery.Visible = false;
            }
        }

        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        {
            var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

            if (Mylist.Count() == 0)
                return;
            foreach (var MylistItem in Mylist)
            {
                ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                FillData2DropDownList(MylistItem.作业中心ID, TotalTrees);
                //当作业中心类型为部门的时候，就不加入这个项目
               if (new string[] { "A", "M" }.Contains(MylistItem.类型) == true)
                {
                    DropDownList_CenterId.Items.Add(InsertItem);
                }
            }
        }
        private void BindMyData(int PageIndex)
        {
            int skipAccount = PageIndex * 10;
            var MyQuery = (from a in db.TABC_JC_ChemicalTestingAndMaintenances
                           join b in db.TABC_JS_Meterial on a.材料ID equals b.材料ID
                           where a.作业中心ID == Convert.ToInt32(DropDownList_CenterId.SelectedValue) && a.会计期间ID == Application["accountPeriod"].ToString()
                           select new { 
                           
                           a.材料ID,a.会计期间ID,b.计量单位,a.作业中心ID,a.数量,b.材料名称,a.金额
                                                      
                           }).Skip(skipAccount).Take(10);;
            GridView_SupplementaryMeterial.DataSource = MyQuery;
            GridView_SupplementaryMeterial.DataBind();

            if (MyQuery.Count() == 0)
            {
                Panel_MainQuery.Visible = false;
            }
            else
            {
                Panel_MainQuery.Visible = true;
            }
        }



        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;

            MyQuery = (from a in db.TABC_JC_ChemicalTestingAndMaintenances
                       where a.作业中心ID == Convert.ToInt32(DropDownList_CenterId.SelectedValue) && a.会计期间ID == Application["accountPeriod"].ToString()
                       select a
                          ).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 20) == 0 ? (Totals / 20) : (Totals / 20 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }


    }
}
